#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main(){
	int fd = open("data.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
	if(fd == -1){
		perror("open failed");
		return 1;
	}

	const char* msg = "Hello, open()!\n";
	if(write(fd, msg, strlen(msg)) == -1){
               perror("write failed");
	       close(fd);
	       return 1;
	}

	if(lseek(fd, 0, SEEK_SET) == -1){
		perror("lseek failed");
		close(fd);
		return 1;
	}

	char buffer[100];
	ssize_t n = read(fd, buffer, sizeof(buffer) - 1);
	if(n == -1){
		perror("read failed");
		close(fd);
		return 1;
	}
	buffer[n] = '\0';
	printf("读取内容：%s", buffer);

	close(fd);
	return 0;
}
             
